
package com.jf.cloud.combo.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.combo.mapper.ComboSpuMapper;
import com.jf.cloud.combo.model.ComboSpu;
import com.jf.cloud.combo.service.ComboSpuService;
import com.jf.cloud.combo.service.ComboSpuSkuService;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.product.vo.ComboSpuSkuVO;
import com.jf.cloud.common.product.vo.ComboSpuVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 套装商品项
 *
 * @author zz
 * @date 2022-08-16 09:18:06
 */
@Service
public class ComboSpuServiceImpl implements ComboSpuService {

    @Autowired
    private ComboSpuMapper comboSpuMapper;
    @Autowired
    private MapperFacade mapperFacade;
    @Autowired
    private ComboSpuSkuService comboSpuSkuService;

    @Override
    public PageVO<ComboSpu> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> comboSpuMapper.list());
    }

    @Override
    public ComboSpuVO getByComboSpuId(Long comboProdId) {
        return comboSpuMapper.getByComboSpuId(comboProdId);
    }

    @Override
    public void save(ComboSpu comboSpu) {
        comboSpuMapper.save(comboSpu);
    }

    @Override
    public void update(ComboSpu comboProd) {
        comboSpuMapper.update(comboProd);
    }

    @Override
    public void deleteById(Long comboProdId) {
        comboSpuMapper.deleteById(comboProdId);
    }

    @Override
    public void insertBatch(List<ComboSpuVO> spuList, Long comboId) {
        if (CollUtil.isEmpty(spuList)) {
            return;
        }
        List<ComboSpu> comboSpuList = new ArrayList<>();
        for (ComboSpuVO comboSpuVO : spuList) {
            List<ComboSpuSkuVO> skuListS = comboSpuVO.getSkuList();
            Long price = 0L;
            for (ComboSpuSkuVO list : skuListS) {
                price += list.getMatchingPrice() * comboSpuVO.getLeastNum();
            }
            comboSpuVO.setComboPrice(price);
            comboSpuVO.setType(2);
            comboSpuVO.setStatus(1);
            comboSpuVO.setComboId(comboId);
            ComboSpu comboSpu = mapperFacade.map(comboSpuVO, ComboSpu.class);
            comboSpuList.add(mapperFacade.map(comboSpuVO,ComboSpu.class));
            comboSpuMapper.save(comboSpu);
            //保存sku信息
            for (ComboSpuSkuVO comboSpuSkuVO : comboSpu.getSkuList()) {
                comboSpuSkuVO.setSkuStatus(comboSpuVO.getStatus());
            }
            comboSpuSkuService.insertBatch(comboSpu.getSkuList(),comboSpu.getComboSpuId());
        }
//        comboSpuMapper.insertBatch(comboSpuList, comboId);
    }

    @Override
    public void deleteBatch(List<Long> comboSpuIdList) {
        comboSpuMapper.deleteBatch(comboSpuIdList);
    }

    @Override
    public List<ComboSpuVO> comboSpuByComboId(Long comboId) {
        return comboSpuMapper.comboSpuByComboIds(Arrays.asList(comboId));
    }

    @Override
    public void comboSpuOfflineDispose(List<Long> spuIdList) {
        if (spuIdList.size() != 0 && Objects.nonNull(spuIdList)){
            comboSpuMapper.batchComboSpuStatus(spuIdList);
        }
    }
}
